// index.js
// 获取应用实例
const app = getApp()
const http = require('../../api/http.js')

Page({
  data: {
    userInfo: null,
  },
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo');
    if(userInfo) {
      this.setData({
        userInfo: userInfo,
      })
    }
  },
  logIn() {
    wx.login({
      success: res => {
        if(res.code) {
          wx.request({
            url: "http://101.43.48.82:4399/api/login?code=" + res.code,
              success: response => {
                wx.setStorageSync("token", response.data);
              }
            })
        }
      }
    })
    wx.getUserProfile({
      desc: '请登录',
      success: res => {
        this.setData({
          userInfo: res.userInfo
        })
        wx.setStorageSync('userInfo', this.data.userInfo)
      }
    })
  },
  logOut() {
    this.setData({
      userInfo: null
    })
    wx.setStorageSync('userInfo', this.data.userInfo)
    wx.removeStorageSync('token');
  }
})
